package com.tn.controller.producer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.tn.controller.alarm.common.constants.Constants;
import com.tn.controller.alarm.common.model.InputVO;
import com.tn.controller.alarm.common.model.OutputVO;
import com.tn.controller.alarm.common.model.external.subsystem.vo.WeighInstructionsVO;
import com.tn.controller.alarm.common.model.external.subsystem.vo.WeightPrintVO;
import com.tn.service.external.subsystem.WeighRestfulService;
import com.tn.utils.CheckParamUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName WeighRestful
 * @Description TODO
 * @Author Yezhongzhu
 * @Date 2021/12/3 14:06
 * @Version v1.0
 **/
@RestController
@RequestMapping("/aucy/")
@Slf4j
public class WeighRestful {
    @Autowired
    private WeighRestfulService weighRestfulService;

    @RequestMapping(value = "acceptTruckWeighingData",consumes={"application/json;charset=utf-8",
            "application/x-www-form-urlencoded;charset=utf-8",
            "application/x-www-form-urlencweighLogInfooded;charset=GB2312"},
            produces = "application/json;charset=utf-8")
    public String acceptTruckWeighingData(@RequestBody String body) {
        InputVO<WeighInstructionsVO> inputVO = JSON.parseObject(body,new TypeReference<InputVO<WeighInstructionsVO>>(){});
        if (!CheckParamUtil.checkWeighParam(inputVO)) {
            OutputVO outputVO = new OutputVO();
            outputVO.setInfo(Constants.OtherSys.STATUS.PARAM.getMessage());
            outputVO.setStatus(Constants.OtherSys.STATUS.PARAM.getCode());
            return JSON.toJSONString(outputVO);
        }
        WeightPrintVO weightPrintVO = weighRestfulService.insertTruckWeighData(inputVO.getData());
        OutputVO<WeightPrintVO> outputVO = new OutputVO();
        outputVO.setData(weightPrintVO);
        WeighInstructionsVO vo = inputVO.getData();
        return JSON.toJSONString(outputVO);
    }
}
